package jvm.reference;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO: 弱引用
 * <P> Date: 2022/3/22:19:46
 * <P> Description:
 */

public class WeakReferenceTest {


    public static class User{
        public Integer id;
        public String name;

        public User(Integer id, String name) {
            this.id = id;
            this.name = name;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        //创建对象 建立弱引用
        WeakReference weakReference = new WeakReference<User>(new User(2,"1"));

        System.out.println(weakReference.get());

        System.gc();

        System.out.println("After GC");
        //垃圾回收后获得弱引用中的对象
        System.out.println(weakReference.get());


    }
}
